package com.example.myapplication9.week4.jsjf;


import com.example.myapplication9.week6.jsjf.exceptions.ElementNotFoundException;

/**
 * LinkedUnorderedList represents Translation singly linked implementation of an
 * unordered list.
 *
 * @author Lewis and Chase
 * @version 4.0
 */
public class LinkedUnorderedList<T> extends LinkedList<T> 
         implements UnorderedListADT<T>
{
    /**
     * Creates an empty list.
     */
     public LinkedUnorderedList()
    {
        super();
    }

    /**
     * Adds the specified element to the front of this list.
     *
     * @param element the element to be added to the list
	 */
    @Override
    public void addToFront(T element)
    {
        LinearNode<T>list1=new LinearNode<T>(element);
        LinearNode<T> list=head;
        if(head==null) {
            head = tail = list1;
count++;
        }else {
            LinearNode<T> list2 = head;
            while (list2.getNext() != null) {
                list2 = list2.getNext();
            }
            list1.setNext(head);
            head = list1;
            tail = list2;
        }
            count++;

       }
	
	/**
     * Adds the specified element to the rear of this list.
     *
     * @param element the element to be added to the list
	 */
    @Override
    public void addToRear(T element)
    {
        LinearNode <T>list=new LinearNode<T>(element);
        if(isEmpty())
        {
            head=tail=list;
        count++;
        }
        else {
            tail.setNext(list);
            tail = list;
        }
        count++;
    }
	
	
    /**
     * Adds the specified element to this list after the given target.
     *
     * @param  element the element to be added to this list
	 * @param  target the target element to be added after
	 * @throws ElementNotFoundException if the target is not found
	 */
    @Override
    public void addAfter(T element, T target)
    {
        LinearNode<T>current=null;
        LinearNode<T>current1=head;
         LinearNode<T>list=new LinearNode<T>(element);
        while (current1.getNext() != null&&current1.getElement()!=target)
        {
            current1=current1.getNext();
            current=current1.getNext();
            }
        if(current1.getNext()==null)
        {
            tail.setNext(list);
            tail=list;
            count++;
        }
        else
        {
            current1.setNext(list);
            list.setNext(current);
            count++;
        }
    }
    }

